<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page import="com.hnas.core.util.PropertyUtil"%>
<%@page import="com.hnas.core.dto.DataBean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Vip产品信息</title>
<script type="text/javascript">
</script>
</head>
<body>
	<!-- 标题 -->
	<div class="titleBar m_b_20">
		<div class="tit fl b_l_e74a5f">
			<h3 class="cn">Vip产品信息</h3>
			<span class="en">vip product information</span>
		</div>
		<div class="fr"></div>
	</div>
	<!-- end 标题 -->

	<form method="post" action="${pageContext.request.contextPath }/backend/vip/product/search" name="searchForm"  >
  	<input type="hidden" name="pageNo" value="${dto.pageNo}">
  	<input type="hidden" name="totalCount" value="${dto.totalCount}">
	<!-- 查询 -->
	<div class="box_03 p_20 m_b_20">
		<table class="tb_style02" width="100%">
   			<tr>
	        <th>产品名称</th>
	        <td><input name="productName" value="${dto.productName}" style="width:200px;" class="input_style2" type="text" placeholder=""></td>
	        <th>是否可用</th>
	        <td>
	        	<select id="productDisabled" name="productDisabled" style="width: 70px;" class="select ui-widget-content ui-corner-all input_style2">
	        		<option value="" ></option>
	        		<option value="0" <c:if test="${dto.productDisabled == 0}"> selected="selected"</c:if> >否</option>
	        		<option value="1" <c:if test="${dto.productDisabled == 1}"> selected="selected"</c:if> >是</option>
	        	</select>
	        </td>
	        <th>产品类型</th>
	        <td>
	        	<select id="productType" name="productType" style="width: 70px;" class="select ui-widget-content ui-corner-all input_style2">
	        		<option value="" ></option>
	        		<c:forEach items="${productTypeMap }" var="o">
	 					<option value="${o.key }" <c:if test="${o.key == dto.productType}"> selected="selected"</c:if> >${o.value }</option>
	 				</c:forEach>
	        	</select>
	        </td>
	      	</tr>
	        <tr>
	            <th>每页显示</th>
	            <td colspan="5">
	            	<input style="width:50px;" class="input_style2" type="text" placeholder="" id="pageSize" name="pageSize" value="${dto.pageSize}"> 条
	                <span style="margin-left:170px;">
	                	<input style="width:110px;" class="btn_brown02 m_r_12" type="button" name="searchData" id="searchData" value="查  询">
	               		<input style="width:110px;" class="btn_brown02 m_r_12" type="button" name="addData" id="addData" value="添 加">
	                </span>
	            </td>
	         </tr>
		</table>
	</div>
	<!-- end 查询 -->
</form>
<%
boolean incDisplayTable = true;
boolean incEditTable = false;
boolean incUploadDiv = true;
boolean incBatchDelete = false;
boolean incDisplayStatus =  true;
String uploadTmp="resources.xls";
%>

<div class="m_b_20">
	<table class="tb_style05" width="100%" style="table-layout:fixed;">
		<thead>
		<tr>
			<c:forEach items="${beans}" var="o">
			<c:if test="${o.display && !o.hidden}"><th>${o.title}</th></c:if>
			</c:forEach>
			<th width="100">操作</th>
		</tr>
		</thead>
		<tbody>
			<c:forEach items="${pg.list}" var="o">
			<tr>
				<c:forEach items="${beans}" var="o1">
					<c:if test="${o1.display && !o1.hidden}">
					<td style="word-wrap:break-word;"><%=String.valueOf(PropertyUtil.getValue(pageContext.getAttribute("o"),((DataBean)pageContext.getAttribute("o1")).getField())).replace("false","否").replace("true","是")%></td></c:if>
				</c:forEach>
					<td>
						<a href="#" class="c_3366cc btn editData" id="${o.id }">编辑</a>&nbsp;&nbsp;
						<%-- <a href="#" class="c_3366cc btn deleteData" id="${o.id }">删除</a> --%>
					</td>
			</tr>
			</c:forEach>
		</tbody>
	</table>
	<div class="searpage" align="right"><div>${pg.pageStr}</div></div><br/>
</div>

<!-- 添加start -->
<div class="editDataDiv" id="editDIV" title="编辑产品信息" style="display: none;" >
	<form action="#" id="editFm" method="post" name="editFm" class="editFm" >
		<input type="hidden" id="dealId" name = "id" value=""/>
		<table >
			<tr>
 				<td width="25%" align="left"> 
 					<label>产品名称：</label>
 				</td>
 				<td><input type="text" id="productNameInput" name="productName" />
 				<font color="red"> *</font></td> 
  			</tr>
  			<tr>
 				<td width="25%" align="left"> 
 					<label>产品类型：</label>
 				</td>
 				<td><select id="productTypeSelect" name="productType" style="width: 70px;" class="select ui-widget-content ui-corner-all input_style2">
	 					<c:forEach items="${productTypeMap }" var="o">
	 						<option value="${o.key }" >${o.value }</option>
	 					</c:forEach>
	 				</select>
 				</td>
 			</tr>
  			<tr>
 				<td width="25%" align="left"> 
 					<label>优惠方式：</label>
 				</td>
 				<td><select id="discountTypeSelect" name="discountType" style="width: 100px;" class="select ui-widget-content ui-corner-all input_style2">
	 					<c:forEach items="${discountTypeMap }" var="o">
	 						<option value="${o.key }" >${o.value }</option>
	 					</c:forEach>
	 				</select>
 				</td> 
  			</tr>
  			<tr>
 				<td width="25%" align="left"> 
 					<label>优惠数额：</label>
 				</td>
 				<td><input type="text" id="discountValueInput" name="transDiscountValue" style="width:100px;"><label id="unit">折</label>
 				<font color="red"> *</font></td> 
  			</tr>
  			<tr>
 				<td width="25%" align="left"> 
 					<label>产品周期：</label>
 				</td>
 				<td><input type="text" id="periodValueInput" name="periodValue" style="width:60px;">
 					<select id="periodTypeSelect" name="periodType" style="width: 70px;" class="select ui-widget-content ui-corner-all input_style2">
 					<c:forEach items="${periodTypeMap }" var="o">
 						<option value="${o.key }" >${o.value }</option>
 					</c:forEach>
 					</select>
 					<font color="red"> *</font>
 				</td> 
  			</tr>
  			<tr>
 				<td width="25%" align="left"> 
 					<label>产品价格：</label>
 				</td>
 				<td><input type="text" id="productPriceInput" name="productPriceStr">元
 				<font color="red"> *</font></td> 
  			</tr>
  			<tr id="trValidEndTime" style="display:none;">
 				<td width="25%" align="left"> 
 					<label>有效截止时间：</label>
 				</td>
               	<td>
               		<input type="text" style="width:160px;" name="validEndTime" id="validEndTime" style="width:100px;" class="input_style2"/>
                </td>
  			</tr>
  			<tr>
 				<td width="25%" align="left"> 
 					<label>是否体验产品：</label>
 				</td>
 				<td>
 				<input type="hidden" id="experienceInput" name="experience" />
 				<input type="radio" id="expY" name="expCheckRadio" checked="checked" ><label for="expY">是</label>
 				<input type="radio" id="expN" name="expCheckRadio" ><label for="expN">否</label> </td> 
  			</tr>
			<tr>
 				<td width="25%" align="left"> 
 					<label>是否可用：</label>
 				</td>
 				<td>
 				<input type="hidden" id="disabledInput" name="disabled" />
 				<input type="radio" id="y" name="checkRadio" checked="checked" ><label for="y">是</label>
 				<input type="radio" id="n" name="checkRadio" ><label for="n">否</label> </td> 
  			</tr>
  			<%-- <tr>
 				<td width="25%" align="left"> 
 					<label>选择特权：</label>
 				</td>
 				<td>
 					<input type="hidden" id="privilegesStr" name="privilegesStr" />
 					<c:forEach items="${privileges}" var="o">
 						<input type="checkbox" name="privilegeCheck" id="${o.id }"/><label for="${o.id }">${o.name }</label>
 					</c:forEach>
 				</td> 
  			</tr> --%>
  			<tr>
 				<td width="25%" align="left"> 
 					<label>产品描述：</label>
 				</td>
 				<td><textarea id="descInput" name="description" rows="3" style="width:200px;"></textarea> </td> 
  			</tr>
			<!-- <tr>
      			<td colspan="2">
          			<p class="validateTips_deal" align="center"></p>
      			</td>
    		</tr> -->
		</table>
	</form>
</div>
<!-- 添加end -->
<script>
	function gotoJumpPage(){
		var jumpNum= $("#jumpNum").val();
		$("#search_page_size").val($("#id_page_size").val());
		gotoPage(jumpNum);
		return false;
	}
	$(function(){
		$(".showDetail").click(function(){
			var id = $(this).attr("id");
			window.location.href = "${pageContext.request.contextPath }/backend/vip/productPrice/search/" + id;
		});
		/** 添加 **/	 
		$("#addData").click(function(){
			doReset(document.editFm);
			$("#editDIV").dialog("open");
		});

		/** 编辑 **/
		$(".editData").click(function(){
			var id = $(this).attr("id");
			var editURL = "${pageContext.request.contextPath }/backend/vip/product/show/" + id;
			$.ajax({
				url:editURL,
				type: 'POST',
				error: function(XMLHttpRequest, textStatus, errorThrown){
					//alert('加载数据异常。' + textStatus + "/" + errorThrown);
					 $.dialog.alert({title:"保存",content:"加载数据异常。"});
				},
				success: function(data){
					fillDIV(data);
				}
			});
			$("#editDIV").dialog("open");
		});
		$("#editDIV").dialog({
			autoOpen : false,
			bgiframe: true, 
			height : 500,
			width : 400,
			modal : true,
			resizable :false,
	       	hide: '',
	        show:'',
	        zIndex:1000,
			buttons : {
				"保 存" : function() {
					var bValid = doValid();
					if(bValid){
						var isAdd = ($("#dealId").val() == null) || ($.trim($("#dealId").val()) == "")||($.trim($("#dealId").val()) == "0");
						var myAddUrl = "${pageContext.request.contextPath }/backend/vip/product/add";
						var mySaveUrl = "${pageContext.request.contextPath }/backend/vip/product/save";
						var url = isAdd?myAddUrl:mySaveUrl;
						if($("#y").is(":checked")){
							$("#disabledInput").val(1);
						}else{
							$("#disabledInput").val(0);
						}
						if($("#expY").is(":checked")){
							$("#experienceInput").val(0);
						}else{
							$("#experienceInput").val(1);
						}
						$.ajax({
							url:url,
							type: 'POST',
							data: $("#editFm").serialize(),
							error: function(XMLHttpRequest, textStatus, errorThrown){
							//alert('保存异常。' + textStatus + "/" + errorThrown);
							 $.dialog.alert({title:"保存",content:"保存异常"});
							},
							success: function(data){
								if(data.result==1) {
									$("#editDIV").dialog("close");
									if (isAdd) {
										//alert('添加成功.');
										 $.dialog.alert({title:"保存",content:"添加成功"});
									}
									else {
										//alert('修改成功.');
										$.dialog.alert({title:"保存",content:"修改成功"});
									}
									query();
								} else {
									$.dialog.alert({title:"保存",content:"保存失败："+ data.errorMessage});	
								}
							}
						});  
					}
				}, 
				"取 消" : function() {
					$(this).dialog("close");			
				}
			},
			close : function() {
				$(this).dialog("close");
			}
		});
		function fillDIV(data){
			$("#dealId").val(data.id);
			$("#productNameInput").val(data.productName);
			$("#productTypeSelect option").each(function(){
				if($(this).val() == data.productType){
					$(this).attr("selected","selected");
				}
			})
			$("#discountTypeSelect option").each(function(){
				if($(this).val() == data.discountType){
					$(this).attr("selected","selected");
				}
			})
			$("#discountValueInput").val(data.transDiscountValue);
			if(data.discountType == 0){
				$("#unit").text("折");
			}else if(data.type == 1){
				$("#unit").text("元");
			}
			$("#periodValueInput").val(data.periodValue);
			$("#periodTypeSelect option").each(function(){
				if($(this).val() == data.periodType){
					$(this).attr("selected","selected");
				}
			})
			$("#productPriceInput").val(data.productPriceStr.substr(0,data.productPriceStr.length-1));
			console.log(data);
			if(data.disabled == 0){
				$("#n").attr("checked","checked");	
			}else{
				$("#y").attr("checked","checked");	
			}
			if(data.periodType == 0){
				$("#trValidEndTime").hide();
			}else if(data.periodType == 1){
				$("#trValidEndTime").show();
			}
			$("#descInput").val(data.description);
			$("#validEndTime").val(data.validEndTime);
			
			if(data.experience == 0){
				$("#expY").attr("checked","checked");	
			}else{
				$("#expN").attr("checked","checked");	
			}
			
		}
		$("#discountTypeSelect").change(function(){
			var val = $(this).val();
			if(val == 0){
				$("#unit").text("折");
			}else if(val == 1){
				$("#unit").text("元");
			}
		})
		$("#periodTypeSelect").change(function(){
			var val = $(this).val();
			if(val == 0){
				$("#trValidEndTime").hide();
			}else if(val == 1){
				$("#trValidEndTime").show();
			}
		})
		function doValid(){
			var productName = $("#productNameInput").val();
			if(productName == null || $.trim(productName) == ''){
				$.dialog.alert({title:"错误",content:"产品名称不能为空！",objCtrl:"#productNameInput"});
				return false;
			}
			var discountValue = $("#discountValueInput").val();
			if(discountValue == null || $.trim(discountValue) == ''){
				$.dialog.alert({title:"错误",content:"优惠数额不能为空！",objCtrl:"#discountValueInput"});
				return false;
			}else{
				var patrn = /^\d+(\.\d+)?$/;
	            if (!patrn.exec(discountValue)) {
	                    $.dialog.alert({title:"错误",content:"优惠数额必须是数字！",objCtrl:"#discountValueInput"});
	    				return false;
	            }
			}
			var periodValue = $("#periodValueInput").val();
			if(periodValue == null || $.trim(periodValue) == ''){
				$.dialog.alert({title:"错误",content:"周期数值不能为空！",objCtrl:"#periodValueInput"});
				return false;
			}else{
				var patrn = /^\d+(\.\d+)?$/;
	            if (!patrn.exec(periodValue)) {
	                    $.dialog.alert({title:"错误",content:"周期数值必须是数字！",objCtrl:"#discountValueInput"});
	    				return false;
	            }
			}
			var productPriceInput = $("#productPriceInput").val();
			if(productPriceInput == null || $.trim(productPriceInput) == ''){
				$.dialog.alert({title:"错误",content:"产品价格不能为空！",objCtrl:"#productPriceInput"});
				return false;
			}else{
				var patrn = /^\d+(\.\d+)?$/;
	            if (!patrn.exec(productPriceInput)) {
	                    $.dialog.alert({title:"错误",content:"产品价格必须是数字！",objCtrl:"#discountValueInput"});
	    				return false;
	            }
	       
			}
/* 			var periodType = $("#periodTypeSelect").find("option:selected").val();
			if(periodType == 1){
				var valieEndTime = $("#validEndTime").val();
				if(valieEndTime == null || $.trim(valieEndTime) == ''){
					$.dialog.alert({title:"错误",content:"有效期截止时间不能为空！"});
					return false;
				}
			} */
			return true;
		}
		
		$("#searchData").click(function(){
			
			/** 校验输入页数是否非法  **/
			var pageSize=$("#pageSize").val();
			var reg = new RegExp("^[0-9]*$");
			if($.trim(pageSize)!=""){
				if(!reg.test($.trim(pageSize))){
					alert("每页显示数请输入非负整数");
	 				return false;
				}
			}
			if($.trim(pageSize)==""){
				alert("每页显示数不应许为空");
				return false;
			}
			
			doSubmit();
		});
		$("#validEndTime").datepicker({
			dateFormat:'yy-mm-dd',
			changeYear: true,
			changeMonth: true,
			yearRange: "1900:2049" 
		});
	});
</script>
</body>

</html>